package hu.kisprof.quizServer.controller;

import hu.kisprof.quiz.model.bean.Edu;
import hu.kisprof.quiz.model.bean.Player;
import hu.kisprof.quiz.model.bean.GameQuestion;
import hu.kisprof.quiz.model.bean.Question;
import hu.kisprof.quiz.model.bean.Topic;
import hu.kisprof.quizServer.model.QuizServerDAO;

import java.net.InetAddress;
import java.sql.SQLException;
import java.util.Map;
import java.util.Timer;
import java.util.TreeSet;

import javax.swing.table.DefaultTableModel;

public class QuizServerController {
	private QuizServerDAO dao = new QuizServerDAO();
	
	public QuizServerController() throws SQLException{
		System.out.println("QuizServer v0.3 Started");
		new QuizServerIncomingConnectionListener(this).start();
		Timer timer =new Timer(true);
		timer.schedule(new CleanUpTask(this), 0, 2000);
	}
	
	
	public Map<String,Integer> getRooms(){
		return dao.getRoomsStringMap();
	}
	
	public boolean createRoom(String name){
		return dao.createRoom(name);
	}
	
	public boolean createRoom(String name,Player ply){
		return dao.createRoom(name, ply);
	}
	public TreeSet<String> getRoomPlayers(String roomName){
		return dao.getRoomPlayers(roomName);
	}
	
	public boolean isPlayerExist(String username) {
		return dao.isPlayerExist(username);
	}
	
	public boolean registerPlayer(Player player) {
		return dao.registerPlayer(player);
	}
	
	public Object login(String usrname, String password, InetAddress inetAddress, int port) {
		return dao.login(usrname,password, inetAddress, port);
	}
	
	
	public static void main(String[] args){
		try {
			new QuizServerController();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean joinRoom(String roomName, Player ply) {
		return dao.joinRoom(roomName, ply);
		
	}

	public void leaveRoom(String roomName, Player ply) {
		dao.leaveRoom(roomName, ply);
	}


	public void cleanUsers() {
		dao.cleanUsers();
	}


	public void cleanRooms() {
		dao.cleanRooms();
	}


	public void challangePlayer(String challenger,String challenged, String message, int difficulty) {
		dao.challangePlayer(challenger,challenged,message,difficulty, this);
		
	}


	public boolean isPlayerOnline(String username) {
		return dao.isPlayerOnline(username);
		
	}





	public void logout(String username) {
		dao.logout(username);
	}


	public GameQuestion getGameQuestion(int difficulty) {
		return dao.getGameQuestion(difficulty);
	}


	public void playerReady(String username,boolean answer) {
		dao.playerReady(username,answer);
		
	}
	
	public void playerAnswered(String username,int questionId, boolean answer){
		dao.playerAnswered(username,questionId,answer);
	}


	public Player playerStatistic(String username) {
		return dao.playerStatistic(username);
	}


	public TreeSet<String> getQuestions() {
		return dao.getQuestions();
	}


	public TreeSet<Topic> getTopics() {
		return dao.getTopics();
	}


	public void addTopic(String name) {
		dao.addTopic(name);
		
	}


	public void addQuestion(String question, String good, TreeSet<Topic> topics, int difficulty,	TreeSet<String> badAnswers) {
		dao.addQuestion(question,good,topics,difficulty,badAnswers);

	}


	public TreeSet<Edu> getEdus() {
		return dao.getEdus();
	}


	public String checkZipCode(int zipcode) {
		return dao.checkZipCode(zipcode);
	}


	public void deleteQuestion(int id) {
		dao.deleteQuestion(id);
	}


	public void editQuestion(int questionId, String question, String goodAns,
			TreeSet<Topic> topics, int difficulty, TreeSet<String> badAnswers){
		dao.editQuestion(questionId,question,goodAns,topics,difficulty,badAnswers);
	}


	public Question getQuestion(int id) {
		return dao.getQuestion(id);
	}


	public void addPoint(String ply,int p) {
		dao.addPoint(ply,p);
		
	}


	public int getPoints(String plyName) {
		return dao.getPoints(plyName);
	}


	public DefaultTableModel getStatistic(int i) {
		return dao.getStatistic(i);
	}


	public TreeSet<String> getAllPlayers() {
		return dao.getAllPlayers();
	}


	public void setAdmin(String username) {
		dao.setAdmin(username);
		
	}


	public void deletePlayer(String username) {
		dao.deletePlayer(username);
	}




	
}
